package com.xb.service;

import com.xb.dao.ArticleDao;
import com.xb.entity.Article;
import com.xb.entity.Favorite;
import com.xb.entity.PageResult;
import com.xb.entity.User;
import com.xb.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ArticleService {

    @Autowired
    ArticleDao articleDao;

    @Autowired
    ArticleMapper articleMapper;

    /**
     * 查询文章列表
     * @param search 文章标题
     * @param page 当前页
     * @return 响应结果集
     */
    public Page<Article> findArticle(String search, Integer page) {
        return articleDao.findByTitleLike("%" + search + "%", PageRequest.of(page -1, 3));
    }

    /**
     * 文章详情页 查询文章详情
     * @param userId 用户ID
     * @param articleId 文章ID
     * @return 文章信息和关注用户
     */
    public Map<String, Object> updateArticle(Long userId,Long articleId) {
        // map 存放详情信息
        Map<String, Object> map = new HashMap();
        // list保存相关用户
        ArrayList<User> list = new ArrayList<>();
        Article article = articleDao.findById(articleId).get();
        if (article.getUserId() != userId) {
            // 查询文章作者不是登录用户，浏览量+1
            articleDao.updateArticleBrowseCountById(articleId, article.getBrowseCount() + 1);
        }
        // 更新收藏数
        article.setFavorites(articleMapper.countUserFavoriteByArticleId(articleId));
        //
        List<User> users = articleMapper.findFavoriteArticleAndMyFocus(userId, articleId);
        // 查询登录用户是否收藏了该文章
        if (0 == articleMapper.findFavorite(userId, articleId)) {
            article.setFavorite(false);
        } else article.setFavorite(true);
        for (User user : users) {
            list.add(user);
        }
        map.put("articleInfo", article);
        map.put("favorite", list);
        return map;
    }

    public String changFavorite(Long userId, Long articleId, Boolean state) {
        if (articleMapper.findFavorite(userId, articleId)>0) {    // 已收藏
            if (state) {
                return "已收藏";
            } else {
                articleDao.deleteFavorite(userId, articleId);
                return "取消收藏";
            }
        } else {        // 未收藏
            if (state) {
                articleDao.insertFavorite(userId, articleId);
                return "收藏成功";
            } else {
                return "已取消收藏";
            }
        }
    }

    public Boolean isFavorite(Long userId, Long articleId) {
        return articleMapper.findFavorite(userId, articleId) > 0;
    }

    public Page<Article> findMyFavorite(Long userId, String search, Integer page) {
        return articleDao.findFavoriteByTitleLike(userId, "%" + search + "%", PageRequest.of(page - 1, 3));
    }

    public void addArticle(User user, Article article) {
        article.setBrowseCount(0L);
        article.setPublishDate(new Date());
        article.setUserId(user.getId());
        article.setPublishRealName(user.getRealName());
        articleDao.save(article);
    }

    public Integer countFavoriteByArticleId(Long id) {
        return articleDao.countFavoriteByArticleId(id);
    }

    public Article findById(Long id) {
        return articleDao.findById(id).get();
    }
}
